import java.io.IOException;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Random;

import net.minecraft.server.*;

public class SMPAdmin {
	private static Logger a = Logger.getLogger("Minecraft");
	private static SMPAdmin instance;
	private MinecraftServer server;

	private SMPAdmin(MinecraftServer server) {
		this.server = server;
		instance = this;
	}

	public double spawnX()
	{
		return (server.e.m + 0.5D);
	}
	public double spawnY()
	{
		return server.e.d(server.e.m, server.e.o) + 1.5D;
	}
	public double spawnZ()
	{
		return server.e.o + 0.5D;
	}

	public MinecraftServer getServer()
	{
		return this.server;
	}
	
	public static SMPAdmin getInstance(MinecraftServer server) {
		if (instance == null)
			instance = new SMPAdmin(server);
		return instance;
	}

	public static SMPAdmin getInstance() {
		// if we haven't initialized it yet, you'll just get null, so check the return value of this!
		return instance;
	}

	public void msgAll(String str) {
		server.f.a(((hh) (new az(str))));
	}

	public void serverCommand(String str, Player player) {
		server.a(str, ((ea) (player.getControllerInstance())));
	}

	public void save()
	{
		server.e.a(true, ((hn) (null)));
	}

	public void makeOp(String player)
	{
		server.f.e(player);
	}

	public void deOp(String player)
	{
		server.f.f(player);
	}

	public boolean parseChatCommand(Player e, String str)
	{
		if(str.startsWith("/"))
		{
			try {
				String[] split = str.split(" ");
				if(str.toLowerCase().startsWith("/me "))
				{
					str = (new StringBuilder()).append("* ").append(e.name()).append(" ").append(str.substring(str.indexOf(" ")).trim()).toString();
					a.info(str);
					msgAll(str);
				} else if (split[0].equalsIgnoreCase("/reload") && etc.getInstance(server).isAdmin(e)) {
					// d.d.reload(new File("server.properties"));
					etc.getInstance(server).reload();
					a.info("Reloaded users, bans, kits and server.props");
					e.sendMessage("Successfuly reloaded users, bans, kits and server.props");
				} else if(str.toLowerCase().equalsIgnoreCase("/home"))
				{
					a.info((new StringBuilder()).append(e.name()).append(" returned home").toString());
					e.teleport(spawnX(), spawnY(), spawnZ(), 0.0F, 0.0F);
				} else if (split[0].equalsIgnoreCase("/msg")) {
					if (split.length < 3) {
						e.sendMessage("§cCorrect usage is: /msg [player] [message]");
						return true;
					}
					
					Player player = null;
					for (int i = 0; i < server.f.b.size(); i++) {
						Player localds = new Player(server.f.b.get(i));
						if (localds.name().equalsIgnoreCase(split[1])) {
							player = localds;
						}
					}
					
					if (player != null) {
						player.sendMessage("(MSG) <" + e.name() + "> " + combineSplit(2, split, " "));
						e.sendMessage("(MSG) <" + e.name() + "> " + combineSplit(2, split, " "));
					} else {
						e.sendMessage("§cCouldn't find player " + split[1]);
					}
				} else if (split[0].equalsIgnoreCase("/setwp")) {
					if (split.length < 2) {
						e.sendMessage("§cCorrect usage is: /setwp [name]");
						return true;
					}
					etc.getInstance(server).setWaypoint(e, split[1]);
				} else if (split[0].equalsIgnoreCase("/wp")) {
					if (split.length < 2) {
						e.sendMessage("§cCorrect usage is: /wp (player) [name] -- only name is required");
						return true;
					}
					String wpname = split[1];
					String player = e.name();
					if (split.length > 2) {
						player = split[1];
						wpname = split[2];
					}
					etc.Waypoint wp = etc.getInstance(server).getWaypoint(player, wpname);
					if(wp == null)
					{
						e.sendMessage("§cFailed to find a waypoint by that name");
						return true;
					}
					
					a.info(e.name() + " used " + player + "'s wp");
					e.teleport(wp.xLoc, wp.yLoc, wp.zLoc, 0.0F, 0.0F);
				} else if (split[0].equalsIgnoreCase("/listwp")) {
					String player = e.name();
					if ((etc.getInstance(server).listWpForEveryone || etc.getInstance(server).isMod(e)) && split.length > 1) {
						player = split[1];
					}
					e.sendMessage("§cWaypoints: " + etc.getInstance(server).listWaypoints(player));
				} else if (split[0].equalsIgnoreCase("/rmwp")) {
					if (split.length < 2) {
						e.sendMessage("§cCorrect usage is: /rmwp [name]");
						return true;
					}
					etc.getInstance(server).removeWaypoint(e, split[1]);
					e.sendMessage("§cWaypoint removed.");
				} else if (split[0].equalsIgnoreCase("/save")) {
					a.info(e.name() + " saved");
					save();
					msgAll("Saving...");
				} else if (split[0].equalsIgnoreCase("/loc")) {
					a.info(e.name() + " is located at " + e.x() + ", " + e.y() + ", " + e.z());
					e.sendMessage("§cCorrect usage is: /rmwp [name]");
					e.sendMessage("§cCurrect location:" + (int)e.x() + ", " + (int)e.y() + ", " + (int)e.z());
				} else if (split[0].equalsIgnoreCase("/hoobernia")) {
					e.teleport(326.5, 72.0, 229.5, 0.0F, 0.0F);
				} else if (split[0].equalsIgnoreCase("/tp") && (etc.getInstance(server).teleportForEveryone || etc.getInstance(server).isMod(e))) {
					if (split.length < 2) {
						e.sendMessage("§cCorrect usage is: /tp [player]");
						return true;
					}
					
					Player player = null;
					for (int i = 0; i < server.f.b.size(); i++) {
						Player localds = new Player(server.f.b.get(i));
						if (localds.name().equalsIgnoreCase(split[1])) {
							player = localds;
						}
					}
					
					if (e.name().equalsIgnoreCase(split[1])) {
						e.sendMessage("§cYou're already here!");
						return true;
					}
					
					if (player != null) {
						a.info(e.name() + " teleported to " + player.name());
						e.teleport(player.x(), player.y(), player.z(), 0.0F, 0.0F);
					} else {
						e.sendMessage("§cCan't find user " + split[1] + ".");
					}
				} else if(str.toLowerCase().equalsIgnoreCase("/away") && (etc.getInstance(server).awayForEveryone || etc.getInstance(server).isMod(e)))
				{
					java.util.Random random = new Random();
					a.info((new StringBuilder()).append(e.name()).append(" went away").toString());
					double d1 = java.lang.Math.random() * 10000D - 5000D;
					double d2 = java.lang.Math.random() * 10000D - 5000D;
					e.teleport(d1, 80D, d2, random.nextFloat() * 360F, 0.0F);
				} else if(etc.getInstance(server).isAdmin(e))
				{
					java.lang.String s1 = str.substring(1);
					a.info((new StringBuilder()).append(e.name()).append(" issued server command: ").append(s1).toString());
					serverCommand(s1, e);
				} else {
					return true;
				}
			} catch (Exception ex) {
				a.log(Level.SEVERE, "Exception in command handler (Report this to hey0):", ex);
				if (etc.getInstance(server).isAdmin(e))
					e.sendMessage("Exception occured. Check the server for more info.");
				return false;
			}
		} else if (str.startsWith("!")) {
			str = "<" + e.name() + "> " + str;
			a.info(str);
		} else {
			if (etc.getInstance(server).isAdmin(e)) {
				str = etc.getInstance(server).adminColor + "<" + e.name() + ">§F " + str;
			} else if (etc.getInstance(server).isMod(e)) {
				str = etc.getInstance(server).modColor + "<" + e.name() + ">§F " + str;
			} else if (etc.getInstance(server).isBuilder(e)) {
				str = etc.getInstance(server).builderColor + "<" + e.name() + ">§F " + str;
			} else {
				str = etc.getInstance(server).defaultColor + "<" + e.name() + ">§F " + str;
			}
			a.info(str);
			msgAll(str);
		}
		return true;
	}

	public static String combineSplit(int startIndex, String[] string, String seperator) {
		StringBuilder builder = new StringBuilder();
		for (int i = startIndex; i < string.length; i++)
			builder.append(string[i] + seperator);
		builder.deleteCharAt(builder.length() - 1); // remove the extra
		// seperator
		return builder.toString();
	}


}

		/*
		} else if(s.toLowerCase().equalsIgnoreCase("/iron")) {
	if(net.minecraft.server.MinecraftServer.b.containsKey(((java.lang.Object) (e.name()))))
			{
				a.info((new StringBuilder()).append(e.name()).append(" failed to iron!").toString());
				b(((hc) (new ay("\247cYou can't /iron again so soon!"))));
		} else {
		net.minecraft.server.MinecraftServer.b.put(((java.lang.Object) (e.name())), ((java.lang.Object) (java.lang.Integer.valueOf(6000))));
		a.info((new StringBuilder()).append(e.name()).append(" ironed!").toString());
	for(int i1 = 0; i1 < 4; i1++)
		e.a(new ge(ep.m, 1));

		}
		} else
	if(s.toLowerCase().equalsIgnoreCase("/wood"))
		{
	if(net.minecraft.server.MinecraftServer.b.containsKey(((java.lang.Object) (e.name()))))
		{
		a.info((new StringBuilder()).append(e.name()).append(" failed to wood!").toString());
		b(((hc) (new ay("\247cYou can't /wood again so soon!"))));
		} else
		{
	net.minecraft.server.MinecraftServer.b.put(((java.lang.Object) (e.name())), ((java.lang.Object) (java.lang.Integer.valueOf(6000))));
		a.info((new StringBuilder()).append(e.name()).append(" wooded!").toString());
		for(int j1 = 0; j1 < 4; j1++)
			e.a(new ge(ev.y, 1));

		}
	*/
